package mysql

import (
	"gorm.io/gorm"
	"shuqi-novel/novelrpc/novelrpc"
)

type Novel struct {
	gorm.Model
	Title     string `gorm:"type:varchar(30);not null"`
	Chapter   int64  `gorm:"type:int(11);not null"`
	Desc      string `gorm:"type:varchar(100);not null"`
	NovelType string `gorm:"type:varchar(100);not null"`
	UserId    int64  `gorm:"type:int(11);not null"`
}

func (n *Novel) NovelAdd(db *gorm.DB) error {
	return db.Debug().Create(n).Error
}

func (n *Novel) NovelUpdate(db *gorm.DB, id int64) error {
	return db.Debug().Updates(n).Error
}

func NovelList(db *gorm.DB, novel []*novelrpc.NovelList, in *novelrpc.NovelListRequest) []*novelrpc.NovelList {
	db.Debug().Model(&Novel{}).
		Select("novels.*,users.nick_name,novels.id as novel_id").
		Joins("left join users on users.id = novels.user_id").
		Scopes(Paginate(int(in.Page), int(in.PageSize))).Find(&novel)
	return novel
}
